#include "stdio.h"
#include "string.h"
#include "stdlib.h"

enum _RESULT{
	SUCCESS=0,
	FAILE,
};

typedef _RESULT RESULT;

typedef struct _NODE {
	char data[1024];
	struct _NODE *next;
	struct _NODE *pre;
}NODE;

NODE *head, *end;

void print(NODE *nPtr);
void print(NODE *nPtr, int index);
void print(NODE *nPtr, int from, int to);

RESULT insertNode(NODE *nPtr);
RESULT insertNode(NODE *nPtr, int index);

RESULT deleteNode(NODE *nPtr);
RESULT deleteNode(NODE *nPtr, int index);


int main(int argc, char *argv[])
{
	head = (NODE*)malloc(sizeof(NODE));
	strcpy(head->data,"head");

	end = (NODE*)malloc(sizeof(NODE));
	strcpy(end->data, "end");

	head->next = end;
	head->pre = end;
	end->next = head;
	end->pre = head;

	puts("------------------------");
	print(head);
	puts("-------- insert --------");

	NODE *temp1, *temp2, *temp3;
	temp1 = (NODE*)malloc(sizeof(NODE));
	strcpy(temp1->data, "A");

	temp2 = (NODE*)malloc(sizeof(NODE));
	strcpy(temp2->data, "B");

	temp3 = (NODE*)malloc(sizeof(NODE));
	strcpy(temp3->data, "C");

	insertNode(temp1);
	insertNode(temp2);
	insertNode(temp3);
	print(head, 0,10);

	puts("-------- delete --------");
	deleteNode(NULL);
	print(head);
	
	return EXIT_SUCCESS;
}


void print(NODE *nPtr)
{
	NODE *tPtr = nPtr;

	printf("%s", tPtr->data);
	while(tPtr->next != end)
	{
		tPtr = tPtr->next;
		printf("->\"%s\"",tPtr->data);
	}
	printf("->%s\n",tPtr->next->data);
}

void print(NODE *nPtr, int index)
{
	int i;
	NODE *tPtr = nPtr;
	for (i=0; i<index; ++i)
		tPtr = tPtr->next;
	printf("find : \"%s\"\n",tPtr->data);
}

void print(NODE *nPtr, int from, int to)
{
	int i;
	NODE *tPtr = nPtr;

	for(i=0; i<=to; ++i) 
	{
		if(from<=i && i<=to)
			printf("->\"%s\"", tPtr->data);
		tPtr = tPtr->next;
	}
	puts("");
}

RESULT insertNode(NODE *nPtr)
{
	NODE *tPtr = end;

	nPtr->pre = tPtr->pre;
	tPtr->pre->next = nPtr;
	nPtr->next = end;
	end->pre = nPtr;

	return SUCCESS;
}

RESULT insertNode(NODE *nPtr, int index)
{
	return SUCCESS;
}

RESULT deleteNode(NODE *nPtr)
{
	if(nPtr == NULL) return FAILE;
	NODE *tPtr=nPtr;
	
	while(tPtr->next != nPtr)
		tPtr = tPtr->next;
	tPtr->next = tPtr->next->next;
	tPtr->next->pre = tPtr;
	free(nPtr);
	return SUCCESS;
}

RESULT deleteNode(NODE *nPtr, int index)
{
	return SUCCESS;
}